<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>
        <el-card>
          <el-tabs>
            <!-- 左侧 -->
            <el-tab-pane label="角色管理">
              <el-row style="height:60px">
                <el-button icon="el-icon-plus" type="primary" size="small" @click="showDialog=true">新增角色</el-button>
              </el-row>

              <el-table border :data="list">
                <el-table-column label="序号" align="center" type="index" width="120" prop="id" />
                <el-table-column label="名称" align="center" width="240" prop="name" />
                <el-table-column label="描述" align="center" prop="description" />
                <el-table-column label="操作" align="center">
                  <!-- 作用域插槽 -->
                  <template slot-scope="{row}">
                    <el-button type="success" @click="assgnPerm(row.id)">分配权限</el-button>
                    <el-button type="primary" @click="editRole(row.id)">编辑</el-button>
                    <el-button type="danger" @click="deleteRole(row.id)">删除</el-button>
                  </template>

                </el-table-column>
              </el-table>

              <!-- 分页 -->
              <el-row type="flex" justify="center" align="middle" style="height:60px">
                <el-pagination :current-page="page.page" layout="prev, pager, next" :total="page.total" :page-size="page.pagesize" @current-change="changePage" />
              </el-row>
            </el-tab-pane>

            <!-- 右侧 -->
            <el-tab-pane label="公司信息">
              <el-alert
                title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
                type="info"
                :closable="false"
                :show-icon="true"
              />
              <el-form label-width="120px" style="margin-top:50px">
                <el-form-item label="企业名称">
                  <el-input v-model="formData.name" disabled style="width:400px" />
                </el-form-item>
                <el-form-item label="公司地址">
                  <el-input v-model="formData.companyAddress" disabled style="width:400px" />
                </el-form-item><el-form-item label="电话">
                  <el-input v-model="formData.companyPhone" disabled style="width:400px" />
                </el-form-item><el-form-item label="邮箱">
                  <el-input v-model="formData.mailbox" disabled style="width:400px" />
                </el-form-item><el-form-item label="备注">
                  <el-input v-model="formData.remarks" type="textarea" rows="4" disabled style="width:400px" />
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </h2>
    </div>

    <!-- 弹出层 -->
    <el-dialog :title="bumen" :visible="showDialog">
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button size="small" @click="btnClose">取消</el-button>
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <!-- 放置一个弹层 -->
    <el-dialog :visible="showPerm" title="分配权限" @close="btnPermCancel">
      <!-- 权限 是 一棵树 -->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
        :default-checked-keys="selectChecked"
      />

      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" @click="btnPermOk">确定</el-button>
          <el-button @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      list: [], // 存储角色信息
      page: {
        // 放置页码及相关数据
        page: 1, // 当前页码
        pagesize: 6, // 每页数据
        total: 0 // 记录总数
      },

      formData: {}, // 承载公司数据
      showDialog: false,
      showPerm: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      permData: [],
      selectChecked: [], // 记录当前权限点的标识
      defaultProps: {
        // 定义显示字段的名称
        label: 'name'
      },
      userID: null
    }
  },
  created() {
    this.getRoles() // 获取角色列表
    this.getCompanyInfo()
  },
  methods: {
    async getRoles() {
      const { total, rows } = await getRoles(this.page)
      this.page.total = total
      this.list = rows
    },
    changePage(newpage) {
      // newpage 是最新的页码
      this.page.page = newpage
      this.getRoles()
    },
    // 获取公司数据
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 删除
    async deleteRole(id) {
      try {
        await this.$confirm('确定删除数据吗？')
        await deleteRole(id)
        this.getRoles()
        this.$message.success('删除成功')
        this.page.page = 1
      } catch (error) {
        this.$message(error)
      }
    },
    // 显示编辑
    async editRole(id) {
      this.showDialog = true
      // 根据id 请求数据 做数据回显
      this.roleForm = await getRoleDetail(id)
    },
    btnClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async btnOk() {
      try {
        await this.$refs.roleForm.validate() // 校验表单
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm) // 发起请求，修改数据
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        this.getRoles() // 重新渲染数据
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (error) {
        // this.$notify.error({ title: '提示', message: '错误！', position: 'top-right' })
      }
    },
    // 弹出层
    async assgnPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')

      // 根据id 记录下来  数据回显
      this.userID = id

      // 当前角色所拥有的权限点数据
      const { permIds } = await getRoleDetail(id)
      // console.log(permIds)
      this.selectChecked = permIds // 将当前角色所拥有的角色ID赋值

      this.showPerm = true
    },
    async btnPermOk() {
      // 获取被选择的选项
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.userID })
      this.$message.success('权限修改成功')
      this.showPerm = false
    },
    btnPermCancel() {
      this.showPerm = false
      this.selectChecked = []
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    bumen() {
      return this.roleForm.id ? '编辑部门' : '添加部门'
    }
  }
}
</script>

<style>

</style>
